package com.lhys.controller;


import com.github.pagehelper.PageInfo;
import com.lhys.common.ApiResultMsg;
import com.lhys.common.ResultInfo;
import com.lhys.controller.base.BaseController;
import com.lhys.pojo.AssetDiscardMode;
import com.lhys.service.AssetDiscardModeService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.Authorization;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

/**
 * <p>
 * 资产_报废方式表 前端控制器
 * </p>
 *
 * @author 小七
 * @since 2021-05-06
 */
@Api(tags = "报废方式模块")
@RestController
@RequestMapping("/api/asset-discard-mode")
public class AssetDiscardModeController extends BaseController {
    @Autowired
    private AssetDiscardModeService assetDiscardModeService;

    @PutMapping("/save")
    @ApiOperation(value = "报废方式添加或更新", authorizations = {@Authorization(value = header)})
    public ApiResultMsg saveAssetDiscardMode(@RequestBody AssetDiscardMode assetDiscardMode) {
        Boolean aBoolean = assetDiscardModeService.saveAssetDiscardMode(assetDiscardMode);
        if(aBoolean == null){
            return ApiResultMsg.error("该报废方式标题或编码已存在");
        }
        if (!aBoolean) {
            return ApiResultMsg.error(ResultInfo.ERROR);
        }
        return ApiResultMsg.success(ResultInfo.SUCCESS);
    }

    @DeleteMapping("/delete/{id}")
    @ApiOperation(value = "报废方式删除", authorizations = {@Authorization(value = header)})
    public ApiResultMsg deleteAssetDiscardMode(@PathVariable("id") Integer id) {
        Boolean aBoolean = assetDiscardModeService.delAssetDiscardModeById(id);
        if (!aBoolean) {
            return ApiResultMsg.error(ResultInfo.ERROR);
        }
        return ApiResultMsg.success(ResultInfo.SUCCESS);
    }

    @GetMapping("/getAssetDiscardMode/{id}")
    @ApiOperation(value = "根据id查询报废方式", authorizations = {@Authorization(value = header)})
    public ApiResultMsg getAssetDiscardModeById(@PathVariable("id") Integer id) {
        AssetDiscardMode AssetDiscardMode = assetDiscardModeService.getAssetDiscardModeById(id);
        return ApiResultMsg.success(ResultInfo.SUCCESS, AssetDiscardMode);
    }

    @GetMapping("/list")
    @ApiOperation(value = "报废方式查询分页", authorizations = {@Authorization(value = header)})
    public ApiResultMsg listAssetDiscardModeByPage(AssetDiscardMode assetDiscardMode , @RequestParam(defaultValue = "1") Integer pageNum ,@RequestParam(defaultValue = "10") Integer pageSize) {
        PageInfo<AssetDiscardMode> assetDiscardModeList = assetDiscardModeService.getAssetDiscardModeList(assetDiscardMode , pageNum , pageSize);
        return ApiResultMsg.success(ResultInfo.SUCCESS, assetDiscardModeList);
    }
}

